package com.yms.applete;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.weaver.ast.Var;
import org.springframework.util.CollectionUtils;

import java.applet.Applet;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @version : 1.0
 * @author: yangmingsong
 * @date: 2022/8/10-08-10-15:45
 * @description: spring5-test
 */
public class Test  {




    public static void main(String[] args) {
/*        String s = test2("aacaddddssssfg");
        System.out.println(s);*/
        int[] groupSizes = {3,3,3,3,3,1,3};
        List<List<Integer>> lists = groupThePeople(groupSizes);
        int i = lists.hashCode();
        System.out.println(JSONObject.toJSONString(lists));
    }

    public static String test2(String str){
        char[] chars = str.toCharArray();
        StringBuilder builder = new StringBuilder();
        for(int i=0;i<chars.length;){
            char c = chars[i];
            int zhibiao = 0;
            for(int j=i;j<chars.length;j++){
                if(c == chars[j]){
                    zhibiao++;
                }
                if(j!=chars.length-1 && c != chars[j+1]){
                    break;
                }
            }
            builder.append(c);
            builder.append("_");
            builder.append(zhibiao);
            if(i!=chars.length-1){
                builder.append("_");
            }
            i = i + zhibiao;
        }
        return builder.toString();
    }

    public static List<List<Integer>> groupThePeople(int[] groupSizes) {
//        首先创建一个容器管理需要分组的个数以及list的大小，并且不能重复
        HashMap<Integer, List<Integer>> integerListHashMap = new HashMap<>();
        List<List<Integer>> lists = new ArrayList<>();
        for(int i=0;i<groupSizes.length;i++){
            int size = groupSizes[i];
            if(!integerListHashMap.containsKey(size)){
                integerListHashMap.put(size,new ArrayList<Integer>());
            }
            integerListHashMap.get(size).add(i);
            if(integerListHashMap.get(size).size() == size){
                lists.add(integerListHashMap.get(size));
//                移除已被添加到lists中的list，防止接下来的list被覆盖
                integerListHashMap.remove(size);
            }
        }
        return lists;
    }



}
